<template>
  <basic-form
    ref="unitForm"
    primary-key="unitId"
    v-model="unitForm"
    :rules="unitRules"
    :query-api="unit_queryByIdApi"
    :insert-api="unit_insertApi"
    :update-api="unit_updateApi"
    :formType="formType"
    @insertSuccess="insertSuccess"
    @updateSuccess="updateSuccess"
  >
    <a-form-model-item label="父部门" prop="parentUnitName" ref="parentUnitName">
      <a-input v-model="unitForm.parentUnitName" :disabled="true" />
    </a-form-model-item>
    <a-form-model-item label="编码" prop="unitCode" ref="unitCode">
      <a-input v-model="unitForm.unitCode" />
    </a-form-model-item>
    <a-form-model-item label="名称" prop="unitName" ref="unitName">
      <a-input v-model="unitForm.unitName" />
    </a-form-model-item>
    <a-form-model-item label="全称" prop="unitFullName">
      <a-input v-model="unitForm.unitFullName" type="textarea" placeholder="部门全称......" />
    </a-form-model-item>
    <a-form-model-item label="类型" prop="unitType" ref="unitType">
      <dictionary-select codeItemId="UNIT_TYPE" v-model="unitForm.unitType"></dictionary-select>
    </a-form-model-item>
    <a-form-model-item label="排序" prop="sortNo">
      <a-input v-model.number="unitForm.sortNo" />
    </a-form-model-item>
    <a-form-model-item label="启用" prop="enabledTime">
      <a-date-picker
        placeholder="启用时间"
        style="width:100%"
        v-model="unitForm.enabledTime"
        valueFormat="YYYY-MM-DD"
      />
    </a-form-model-item>
  </basic-form>
</template>
<script>
import {
  unit_queryByIdApi,
  unit_insertApi,
  unit_updateApi,
  getUnit
} from "@/api/org/unit";
import { unitRule } from "@/profile/org/unit";
export default {
  props: {
    currentUnitId: {
      type: String
    },
    formType: {
      //页面类型 区分是否为弹窗页面
      type: String
    },
    formState: {
      //区分页面状态 add update
      type: String
    }
  },
  data() {
    return {
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
      unitRules: unitRule,
      unit_queryByIdApi,
      unit_insertApi,
      unit_updateApi,
      unitForm: {
        unitId: undefined,
        parentId: undefined,
        parentUnitName: undefined
      }
    };
  },
  created() {
    if (this.formState == "add") {
      this.unitForm.parentId = this.currentUnitId;
      this.getParentUnitName();
    } else {
      this.unitForm.unitId = this.currentUnitId;
    }
  },
  methods: {
    getParentUnitName() {
      getUnit(this.unitForm.parentId).then(response => {
        this.unitForm.parentUnitName = response.data.data.unitName;
      });
    },
    insertSuccess() {
      this.$emit("insertSuccess");
    },
    updateSuccess() {
      this.$emit("updateSuccess");
    }
  }
};
</script>